*** Run Synthetic controls on fit lead/lags

	
********************************************************************************
*** Time shifts for synthetic controls
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop

*********************************************************************
*** Fit treated on control leads/ lags to find best fit  
**** Need to do this for each of the treated

local StJoTreat = mofd(mdy(11,16,2004))
*local StJoSettle = mofd(mdy(12,21,2007))

local WheatonTreat = mofd(mdy(10,19,2004))
*local WheatonSettle = mofd(mdy(1,7,2010))

local ElCentroTreat = mofd(mdy(3,20,2006))
*local ElCentroSettle = mofd(mdy(9,20,2010))

local OverlookTreat = mofd(mdy(4,25,2008))
*local OverlookSettle = mofd(mdy(6,21,2012))

local MortonPlantTreat = mofd(mdy(8,27,2008)) 
*local MortonPlantSettle = mofd(mdy(11,20,2012))

local ShandsTreat = mofd(mdy(8,30,2008))
*local ShandsSettle = mofd(mdy(8,8,2013))

local chsTreat = mofd(mdy(1,7,2009))
*local chsSettle= mofd(mdy(8,4,2014))

**** Loop over defendants
local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
foreach dft of local dftlist {

	** Keep only sample and defendant 
	use ipop_controls_panel.dta, clear
	keep if prvdrnum == "`dft'"
	drop control
	append using "controls/`dft'_controls.dta"

	** Tag with group numbers
	merge m:1 prvdrnum using "controls/`dft'_controlgroup_crosswalk.dta", assert(match) nogen

	** Set as time series 
	tsset group dt 

	*** Merge leads and lags
	merge m:1 group using "distance/groupleadlags_`dft'.dta"
	drop _merge mindistance


	** Store group levels in macro
	levelsof group, local(levels)


	*** Replace generate with fitted lead/lags
	sort group dt
	gen out = .


	foreach g of local levels{
		disp `g'
		* Treated group gets no time shift
		if `g' == 999{
			replace out = pmt_amt if group == `g'
			continue
			}

		** Grab the stored shifter 
		qui summarize period if group == `g'
		local shift = r(min)

		** If positive, create lead 
		if `shift' > 0{
			replace out = F`shift'.pmt_amt if group == `g'
		}
		** If negative, create lag 
		if `shift' < 0{
			local shiftvalue = abs(`shift')
			replace out = L`shiftvalue'.pmt_amt if group == `g'
		}

		if `shift' == 0{
			replace out = pmt_amt if group == `g'
		}
	}




	*** Treatment date is filing date for each 
	local treatment_period = ``dft'Treat'

	** Cut missing data from front of time series
	** Which is limited by the highest lag 
	summarize period
	drop if dt < mofd(mdy(1,1,1999)) + abs(r(min))

	*** Cut missing data from end of time series
	*** Which is limited by furthest lead
	*** Data end end of 2016
	summarize period
	drop if dt > mofd(mdy(12, 31, 2016)) - abs(r(max))

	log using synth_`dft'.log, replace 
	synth out out, trunit(999) trperiod(`treatment_period') figure
	log close

************************************
************************************
*** Store fitted values
	matrix fit = e(Y_synthetic)
	keep if group == 999
	svmat fit 
	save "synth_fit_`dft'.dta", replace 
}


